package com.xinxing.city.guard;

import android.annotation.SuppressLint;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;


public class MyService extends Service {
    private static final String TAG = "mytest";
    private MyHandler handler;
    private boolean running = true;
    private static boolean heartbeat2 = false;

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "service oncreate");
        initialize();
        handler = new MyHandler();
        new Thread() {
            @Override
            public void run() {
                super.run();
                while (running) {
                    try {
                        Thread.sleep(30000);
                        Log.e(TAG, "heartBeat:" + heartbeat2);
                        if (heartbeat2) {
                            Log.e(TAG, "心跳");
                            heartbeat2 = false;
                        } else {
                            Log.e(TAG, "执行重启");
                            restartApp();
                            Thread.sleep(20000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, final int startId) {
        int type = intent.getIntExtra("type", 1);
        Log.d(TAG, "the create notification type is " + type + "----" + (type == 1 ? "true" : "false"));
        if (type == 1) {
            createNotificationChannel();
        } else {
            createErrorNotification();
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private boolean initialized;
    protected IntentReceiver intentReceiver;

    private void initialize() {
        if (!initialized) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Options.guardIntentHeartbeat);
            intentReceiver = new IntentReceiver();
            getApplicationContext().registerReceiver(intentReceiver, filter);
            initialized = true;
        }
    }

    class IntentReceiver extends BroadcastReceiver {
        public IntentReceiver() {

        }

        @Override
        public void onReceive(final Context context, final Intent intent) {
            String action = intent.getAction();
            if (action.equals(Options.guardIntentHeartbeat)) {
                heartbeat2 = true;

                Log.d(TAG, "守护进程收到app心跳");
            }
        }
    }


    @SuppressLint("NotificationId0")
    private void createErrorNotification() {
        Notification notification = new Notification.Builder(this).build();
        startForeground(0, notification);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道的id
        String id = "my_channel_01";
        // 用户可以看到的通知渠道的名字.
        CharSequence name = getString(R.string.channel_name);
//         用户可以看到的通知渠道的描述
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, name, importance);
//         配置通知渠道的属性
        mChannel.setDescription(description);
//         设置通知出现时的闪灯（如果 android 设备支持的话）
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
//         设置通知出现时的震动（如果 android 设备支持的话）
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//         最后在notificationmanager中创建该通知渠道 //
        mNotificationManager.createNotificationChannel(mChannel);

        // 为该通知设置一个id
        int notifyID = 1;
        // 通知渠道的id
        String CHANNEL_ID = "my_channel_01";
        // Create a notification and set the notification channel.
        Notification notification = new Notification.Builder(this)
                .setContentTitle("New Message").setContentText("You've received new messages.")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setChannelId(CHANNEL_ID)
                .build();
        startForeground(1, notification);
    }

    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            stopSelf(msg.what);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "5s onDestroy");
        Toast.makeText(this, "this service destroy", Toast.LENGTH_LONG).show();
        stopForeground(true);
    }

    private void restartApp() {
        try {
            Intent in = new Intent();
            in.setClassName("com.xinxing.city.recovery", "com.xinxing.city.recovery.SplashActivity");
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(in);
        } catch (ActivityNotFoundException exception) {
            Log.e(TAG, exception.toString());
        }
    }

}